package generator.model;

import java.io.IOException;
import java.net.MalformedURLException;

import pdf.PdfCreator;

import com.itextpdf.text.DocumentException;

public class GeneratorModel {

	//constants
	public final int SIDE_OFFER_ITEM = 0;
	public final int SIDE_OFFER_SLOGAN = 1;
	public final int MAIN_NEWS = 2;
	public final int MAIN_NEWS_SLOGAN = 3;
	public final int SUPPLEMENT_TITLE = 4;
	public final int SUPPLEMENT_SLOGAN = 5;
	public final int SUPPLEMENT_PICTURE = 6;
	
	//variables
	private EndingGenerator endings;
	private RSSHeadlineGenerator headlines;
	private SupplementGenerator supplements;
	private SideOfferGenerator sideOffers;
	private StandardNewsGenerator standardNews;
	
	private PdfCreator pdfCreator;
	
	private String[] newsItems = new String[7];
	private boolean[] newsItemsLocked = new boolean[7];
	
	public GeneratorModel(PdfCreator pdfCreator, String[] feeds) {
	
		this.pdfCreator =  pdfCreator;
		
		try {
			headlines = new RSSHeadlineGenerator (feeds);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		supplements = new SupplementGenerator("./input/Bilagor.xls");
		sideOffers = new SideOfferGenerator("./input/Bilagor.xls");
		
		endings = new EndingGenerator("./input/Bilagor.xls");
		standardNews = new StandardNewsGenerator("./input/Bilagor.xls");
		//endings.addEnding("");
		
		
	}
	
	private boolean shouldUpdate(int nr) {
		return (!newsItemsLocked[nr] || (newsItems[nr] == null));
	}
	
	private void checkLocks() {
		if(!this.getLock(4)) {
			this.setLock(5, false);
		}
	}
	
	public void updateAll(boolean useStandardNews) {
		
		this.checkLocks();
		
		this.updateHeadline(useStandardNews);
		this.updateSideOffer();
		this.updateSupplement();
		

	}
	
	public void updateHeadline(boolean useStandardNews) {
		
		if(shouldUpdate(MAIN_NEWS)) {
			if(useStandardNews) {
				this.newsItems[MAIN_NEWS] = standardNews.getHeadline().toUpperCase();
			} else {
				this.newsItems[MAIN_NEWS] = headlines.getHeadline().toUpperCase();
			}
		}
		
		if(shouldUpdate(MAIN_NEWS_SLOGAN)) {
			EndingGenerator.EndingType type = EndingGenerator.EndingType.NEUTRAL;
		
			//find out if the news are sad
			String headline = this.newsItems[MAIN_NEWS].toLowerCase();
			if (headline.contains("mörda") 
					|| headline.contains("död") 
					|| headline.contains("dog") 
					|| headline.contains("mord") 
					|| headline.contains("pyroman") 
					|| headline.contains("anmäl") 
					|| headline.contains("sjukhus") 
					|| headline.contains("skandal") 
					|| headline.contains("ihjäl") 
					|| headline.contains("kniv") 
					|| headline.contains("sabotage") 
					|| headline.contains("våld") 
					|| headline.contains("olycka") 
					|| headline.contains("skadad") 
					|| headline.contains("hemsk") 
					|| headline.contains("fruktansvärd") 
					|| headline.contains("katastrof") 
				) 
			{
				type = EndingGenerator.EndingType.SAD;
			}
			
			this.newsItems[MAIN_NEWS_SLOGAN] = endings.getEnding(type).toUpperCase();
		}
	}
	

	
	public void updateSupplement() {
		
		
		if(shouldUpdate(SUPPLEMENT_TITLE)) {
			//randomizes the type of supplement
			supplements.randomizeSupplement();
			
			//get the name and pic for that supplement
			this.newsItems[SUPPLEMENT_TITLE] = supplements.getSupplementName();
			this.newsItems[SUPPLEMENT_PICTURE] = supplements.getSupplementPicture();
		}
		if(shouldUpdate(SUPPLEMENT_SLOGAN)) {
			//get a random tagline for the selected supplement
			this.newsItems[SUPPLEMENT_SLOGAN] = supplements.getTagLine();
		}

	}
	
	public void updateSideOffer() {
		
		if(shouldUpdate(SIDE_OFFER_ITEM)) {
			this.newsItems[SIDE_OFFER_ITEM] = sideOffers.getOfferItem();
		}
		if(shouldUpdate(SIDE_OFFER_SLOGAN)) {
			this.newsItems[SIDE_OFFER_SLOGAN] = sideOffers.getOfferSlogan();
		}

	}

	public void createPDF() throws MalformedURLException, DocumentException, IOException {
		pdfCreator.createPDF(
			this.newsItems[this.SIDE_OFFER_ITEM],
			this.newsItems[this.SIDE_OFFER_SLOGAN],
			this.newsItems[this.MAIN_NEWS],
			this.newsItems[this.MAIN_NEWS_SLOGAN],
			this.newsItems[this.SUPPLEMENT_SLOGAN],
			this.newsItems[this.SUPPLEMENT_PICTURE]
		);
	}
	
	/**
	 * Returns a COPY of the newsItems, for security reasons.
	 * @return the news Items.
	 */
	public String[] getNewsItems() {
		return this.newsItems.clone();
	}
	
	public void setLock(int nr, boolean locked) {
		
		this.newsItemsLocked[nr] = locked;
	}
	
	public boolean getLock(int nr) {
		return this.newsItemsLocked[nr];
	}
}
